class Node:
    def __init__(self,val=0,next=None,random=None):
        self.val=val 
        self.next=next 
        self.random=random 
    
def solve(head):
    if not head: return None
    mp={}
    cur=head 
    while cur:
        mp[cur]=Node(cur.val)
        cur=cur.next 
    for old,new in mp.items():
        if old.next: new.next=mp[old.next]
        if old.random: new.random=mp[old.random]
    return mp[head]

def build(lst):
    head=Node(lst[0][0])
    cur=head
    node_list=[head]
    for i in range(1,len(lst)):
        cur.next=Node(lst[i][0])
        cur=cur.next 
        node_list.append(cur)
    for i in range(len(lst)):
        if lst[i][1]==0 or lst[i][1]:node_list[i].random=node_list[lst[i][1]]
    return node_list[0]

def out(head):
    pos={}
    cur=head 
    k=0
    while cur:
        pos[cur]=k 
        cur=cur.next 
        k+=1
    cur=head 
    res=[]
    while cur:
        rand= pos[cur.random] if cur.random else None 
        res.append([cur.val,rand])
        cur=cur.next 
    return res 

head=build([[7,None],[13,0],[11,4],[10,2],[1,0]])
cp=solve(head)
print(out(cp))
    





    
